package com.karrysong.answerapplication.fragments;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.karrysong.answerapplication.R;
import com.karrysong.answerapplication.beans.Question;
import com.karrysong.answerapplication.utils.LogUtils;
import com.karrysong.answerapplication.utils.SPUtils;

public class AnswerFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {
    private RadioButton rb_option_a;
    private RadioButton rb_option_b;
    private RadioButton rb_option_c;
    private RadioButton rb_option_d;
    private Button okBt;
    private LinearLayout resultLayout;
    private TextView okAn, yourAn;
    private String option = "";
    private RadioGroup rg_base;
    private TextView tv_title;
    Question questBean = null;
    private EditText et_answer;
    private boolean isTest;

    public AnswerFragment(Question question) {
        this.questBean = question;
    }

    @Override
    protected View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_quest, null);
        tv_title = (TextView) view.findViewById(R.id._tv_title);
        rg_base = (RadioGroup) view.findViewById(R.id._rg_base);
        resultLayout = view.findViewById(R.id.resultLin);
        okAn = view.findViewById(R.id.okAn);
        yourAn = view.findViewById(R.id.yourAn);
        okBt = view.findViewById(R.id.et_ok);
        //LogUtils.e( "initView: " + questBean.getQ_type());
        //如果是选择题，找id,设置监听事件
        if (1 == questBean.getType()) {
            rb_option_a = (RadioButton) view.findViewById(R.id._rb_option_a);
            rb_option_b = (RadioButton) view.findViewById(R.id._rb_option_b);
            rb_option_c = (RadioButton) view.findViewById(R.id._rb_option_c);
            rb_option_d = (RadioButton) view.findViewById(R.id._rb_option_d);
            rg_base.setOnCheckedChangeListener(this);
        }
        //如果是判断题，找id,使C,D选项不可见，设置监听事件
        else if (2 == questBean.getType()) {
            rb_option_a = (RadioButton) view.findViewById(R.id._rb_option_a);
            rb_option_b = (RadioButton) view.findViewById(R.id._rb_option_b);
            rb_option_c = (RadioButton) view.findViewById(R.id._rb_option_c);
            rb_option_d = (RadioButton) view.findViewById(R.id._rb_option_d);
            //CD不可见
            rb_option_c.setVisibility(View.GONE);
            rb_option_d.setVisibility(View.GONE);
            //监听事件
            rg_base.setOnCheckedChangeListener(this);
        }
        //如果是简答题，找id,使选项组不可见，使EditText出现。
        else if (3 == questBean.getType()) {
            et_answer = (EditText) view.findViewById(R.id.et_answer);
            et_answer.setVisibility(View.VISIBLE);
            okBt.setVisibility(View.VISIBLE);
            rg_base.setVisibility(View.GONE);
        }

        okBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                questBean.setMyAnswer(et_answer.getText().toString());
                checkAnswer(et_answer.getText().toString());
            }
        });


        return view;
    }

    @Override
    public void initData() {
        tv_title.setText(questBean.getTypeIndex() + "." + questBean.getTitle());
        //如果没有传递数据，则退出
        if (questBean == null) {
            LogUtils.e( "initData: questBean==null");
            return;
        }
//        如果是选择题，对应选项赋值
        if (1 == questBean.getType()) {
            rb_option_a.setText("" + questBean.getaOption());
            rb_option_b.setText("" + questBean.getbOption());
            rb_option_c.setText("" + questBean.getcOption());
            rb_option_d.setText("" + questBean.getdOption());
        }
//        如果是判断题，AB设置为对，错。
        else if (2 == questBean.getType()) {
            rb_option_a.setText("对");
            rb_option_b.setText("错");
        }
//        如果是简答题或者其他,不做数据填充
        else {

        }

        isTest = (boolean) SPUtils.get(getContext(), "isTest", false);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        if (checkedId == rb_option_a.getId()) {
            option = "A";
            if (questBean.getType() == 2){
                option = "对";
            }
            questBean.setMyAnswer(1 + "");
        } else if (checkedId == rb_option_b.getId()) {
            option = "B";
            if (questBean.getType() == 2){
                option = "错";
            }
            questBean.setMyAnswer(2 + "");
        } else if (checkedId == rb_option_c.getId()) {
            option = "C";
            questBean.setMyAnswer(3 + "");
        } else if (checkedId == rb_option_d.getId()) {
            option = "D";
            questBean.setMyAnswer(4 + "");
        }

        checkAnswer(option);

    }

    private void checkAnswer(String option){
        String answer = "";
        String questBeanAnswer = questBean.getAnswer();
        boolean isOk = false;
        if (questBean.getType() == 1){
            if ("1".equals(questBeanAnswer)){
                answer = "A";
            }else if ("2".equals(questBeanAnswer)){
                answer = "B";
            }else if ("3".equals(questBeanAnswer)){
                answer = "C";
            }else if ("4".equals(questBeanAnswer)){
                answer = "D";
            }
        }else if (questBean.getType() == 2){
            if ("1".equals(questBeanAnswer)){
                answer = "对";
            }else if ("2".equals(questBeanAnswer)){
                answer = "错";
            }
        }else {
            answer = questBeanAnswer;
            option = et_answer.getText().toString();
        }


        isOk = answer.equals(option);
        okAn.setText(answer);
        yourAn.setText(option);
        if (isOk){
            yourAn.setTextColor(Color.rgb(76, 175, 80));
        }else {
            yourAn.setTextColor(Color.rgb(233, 30, 99));
        }

        if (!isTest){
            resultLayout.setVisibility(View.VISIBLE);
        }

    }

}
